#include "Room.hh"
#include "../Utils/include/Common.hh"
#include "Fight.hh"
#include <iostream>

using namespace std;

// ===== 普通房间 =====

// 普通房间构造函数
NormalRoom::NormalRoom(int id, const std::string& desc, int floor) : id_(id), desc_(desc), floor_(floor) {}

int NormalRoom::getRoomNumber() const { return id_; }
std::string NormalRoom::getCategory() const { return "Normal Room"; }
std::string NormalRoom::getDescription() const { return desc_; }
void NormalRoom::displayInfo() const { cout << desc_ << endl; }
// 进入房间
void NormalRoom::enter(Player* player) { 
    setVisited(true);
    cout << "This is a safe place, there's nothing here." << endl; 
    Utility::pause(); 
}
// 获取所在楼层
int NormalRoom::getFloor() const { 
    return floor_; 
}

// ===== 出口房间 =====

// 出口房间构造函数
ExitRoom::ExitRoom(int id, const std::string& desc, int nextFloor, int floor) 
    : id_(id), desc_(desc), nextFloor_(nextFloor), floor_(floor) {}

int ExitRoom::getRoomNumber() const { return id_; }
std::string ExitRoom::getCategory() const { return "Exit Room"; }
std::string ExitRoom::getDescription() const { return desc_; }

void ExitRoom::displayInfo() const {
    cout << desc_ << endl;
    if(nextFloor_ > 0) {
        cout << "A passage leads to floor " << nextFloor_ << endl;
    } else {
        cout << "This is the final exit!" << endl;
    }
}

// 进入房间
void ExitRoom::enter(Player* player) {
    setVisited(true);
    cout << "You've reached the exit!" << endl;
    Utility::pause();
    // 实际楼层切换逻辑在Level类中处理
}
// 获取所在楼层
int ExitRoom::getFloor() const { 
    return floor_; 
}

// ===== 物品房间 =====

// 物品房间构造函数
ItemRoom::ItemRoom(int id, const std::string& desc, const std::map<int, Item>& items, int floor) : id_(id), desc_(desc), possibleItems_(items), floor_(floor) {}

// 随机获取一个物品
Item ItemRoom::getRandomItem(){
    if(possibleItems_.empty()) {
        throw std::runtime_error("No items available");
    }
    auto it = possibleItems_.begin();
    std::advance(it, rand() % possibleItems_.size());
    return it->second;
}
// 获取所在楼层
int ItemRoom::getFloor() const { 
    return floor_; 
}

int ItemRoom::getRoomNumber() const { return id_; }

std::string ItemRoom::getCategory() const { return "Item Room"; }
std::string ItemRoom::getDescription() const { return desc_; }
void ItemRoom::displayInfo() const { cout << desc_ << endl; }

// 进入房间
void ItemRoom::enter(Player* player) {
    if (isVisited()) {
        cout << "You've already been here and picked up the items." << endl;
        Utility::pause();
        return;
    }
    setVisited(true);
    cout << "You found a treasure chest!" << endl;
    Item item = getRandomItem();

    if (item.isPicked()) {
        cout << "This item has already been picked up." << endl;
    } else {
        cout << "You got: " << item.getName() << endl;
        item.setPicked(true);
        player->addToInventory(new Item(item));
    }

    Utility::pause();
}

// ===== 怪物房间 =====

// 怪物房间构造函数
MonsterRoom::MonsterRoom(int id, const std::string& desc, Enemy* enemy, int reward, int floor) : id_(id), desc_(desc), enemy_(enemy), reward_(reward), floor_(floor) {}

int MonsterRoom::getRoomNumber() const { return id_; }
std::string MonsterRoom::getCategory() const { return "Monster Room"; }
std::string MonsterRoom::getDescription() const { return desc_; }
void MonsterRoom::displayInfo() const { cout << desc_ << endl; }
// 获取所在楼层
int MonsterRoom::getFloor() const { 
    return floor_; 
}

// 进入房间
void MonsterRoom::enter(Player* player) {
    if (isVisited()) {
        cout << "You've already been here and fought the enemy." << endl;
        Utility::pause();
        return;
    }
    setVisited(true);
    if (enemy_ ->isDefeated()) {
        cout << "You see the remains of a defeated enemy." << endl;
        Utility::pause();
        return;
    }

    cout << "Danger! An enemy has appeared!" << endl;
    Utility::pause();
    Fight fight;
    fight.startFight(player, *enemy_);

    if (player->isAlive() && enemy_->getHealth() <= 0) {
        enemy_->setDefeated(true);
        cout << "You defeated the enemy, earning a reward: " << reward_ << " gold coins (gold coin feature not yet implemented)" << endl;
    }

    Utility::pause();
}

// ===== 治疗房间 =====

// 治疗房间构造函数
HealingRoom::HealingRoom(int id, const std::string& desc, int floor) : id_(id), desc_(desc), floor_(floor) {}

int HealingRoom::getRoomNumber() const { return id_; }
std::string HealingRoom::getCategory() const { return "Healing Room"; }
std::string HealingRoom::getDescription() const { return desc_; }

void HealingRoom::displayInfo() const {
    cout << desc_ << endl;
    cout << "You feel a warm energy filling the room..." << endl;
}

// 进入房间
void HealingRoom::enter(Player* player) {
    setVisited(true);
    player->heal(player->getmaxHealth());
    cout << "You feel completely restored! Your HP is now full." << endl;
    Utility::pause();
}

// 获取所在楼层
int HealingRoom::getFloor() const { 
    return floor_; 
}
